use serde::{Deserialize, Serialize};

use std::time::Duration;
use url::Url;
use std::sync::Arc;
pub async fn article_extractor(url: Arc<Url>) -> Result<ExtractorOutput, ExtractorError> {
    let request_url = "http://article-extractor:3010";
    let client = reqwest::Client::new();
    let json = serde_json::json!({
        "url": url.to_string()
    });
    let duration = Duration::from_secs(5);
    let res = client
        .post(request_url)
        .timeout(duration)
        .json(&json)
        // .header("Content-Type", "application/json")
        // .build();
        .send()
        .await?
        // .expect("readability query error")
        .json::<ExtractorOutput>()
        .await?;
    // .expect("parsing readability error");
    // dbg!(res);
    // dbg!(res.unwrap());
    Ok(res)
}
#[derive(Debug, Serialize, Deserialize)]
pub struct ExtractorOutput {
    pub url: Option<String>,
    pub title: Option<String>,
    pub description: Option<String>,
    pub image:Option<String>,
    pub author: Option<String>,
    pub content: Option<String>,
    pub published: Option<String>,
    pub source: Option<String>,
    pub links: Option<Vec<String>>,
    pub ttr: Option<i64>,
}
use thiserror::Error;
#[derive(Debug, Error)]
pub enum ExtractorError {
    #[error("Parameter url is invalid")]
    InvalidUrl,
    #[error("failed to start extractor process")]
    CommandError(#[from] std::io::Error),
    #[error("Could not parse stderr")]
    ParseResponse,
    #[error("Process timed out")]
    ProcessTimeout,
    // #[error("cannot deserialize json")]
    // SerdeError(#[from] serde_json::Error),
    #[error("cannot deserialize json")]
    SerdeError(),
    #[error("failed to start readability process")]
    RequestError(#[from] reqwest::Error),
    #[error("failed to start readability process")]
    ParserError(#[from] url::ParseError),
    #[error("readability error: {0}")]
    MercuryError(String),
    #[error("")]
    OtherError,
}
